package com.daka.service.impl;

import com.daka.dao.GroupDao;
import com.daka.dao.GroupVolunteerDao;
import com.daka.dao.VolunteerDao;
import com.daka.dao1.MemberOrgDao;
import com.daka.entity.*;
import com.daka.service.GroupVolunteerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by yangx
 * CreateTime: 2016/11/24 14:07
 */

@Service("groupVolunteerService")
public class GroupVolunteerServiceImpl implements GroupVolunteerService {

    private static final Logger logger = LoggerFactory.getLogger(GroupVolunteerServiceImpl.class);

    @Resource
    private GroupDao groupDao;
    @Resource
    private VolunteerDao volunteerDao;
    @Autowired
    private GroupVolunteerDao groupVolunteerDao;
    @Autowired
    private MemberOrgDao memberOrgDao1;


    public int deleteByPrimaryKey(Long id) {
        return groupVolunteerDao.deleteByPrimaryKey(id);
    }

    public int insert(GroupVolunteer record) {
        return groupVolunteerDao.insert(record);
    }

    public GroupVolunteer selectByPrimaryKey(Long id) {
        return groupVolunteerDao.selectByPrimaryKey(id);
    }

    public int updateByPrimaryKeySelective(GroupVolunteer record) {
        return groupVolunteerDao.updateByPrimaryKeySelective(record);
    }

    public int groupVolunteer(Integer max, Integer min) {
        //984468     5448
//        max = 1000000;
        max = 6000;
        min =0;

        Integer seed = 3000;

        int index = (max - min) / seed;
        int a = 0;
        for (int i = 1; i <= index + 1; i++) {

            int pMin = (seed * (i - 1)) + min;
            int pMax = (seed * (i - 1)) + seed + min - 1;
            logger.info("Volunteer 第{" + i + "}批，" + pMin + " 到" + pMax + "条");

            List<MemberOrg1> list = memberOrgDao1.selectList(pMax, pMin);
            logger.info("【查询条数】：{}", list.size());
            List<GroupVolunteer> list1 = new ArrayList<GroupVolunteer>();
            GroupVolunteer groupVolunteer;
            for (MemberOrg1 memberOrg1 : list) {
                groupVolunteer = GroupVolunteer.build(memberOrg1);
                if (memberOrg1.getOrgid() != null) {
                    Group group = groupDao.selectByOldId(Long.valueOf(memberOrg1.getOrgid()));
                    if (group != null) {
                        groupVolunteer.setGroupId(group.getId());
                    }
                }
                if (memberOrg1.getMember() != null) {
                    Volunteer volunteer = volunteerDao.selectByOldId(memberOrg1.getMember());
                    if (volunteer != null) {
                        groupVolunteer.setVolunteerId(volunteer.getId());
                    }
                }
                list1.add(groupVolunteer);
            }
            a = groupVolunteerDao.insertList(list1);
            logger.info("【插入条数】：{}", a);
        }
        return a;
//        List<MemberOrg1> list = memberOrgDao1.selectList(start, end);
//        logger.info("【查询条数】：{}", list.size());
//        List<GroupVolunteer> list1 = new ArrayList<GroupVolunteer>();
//        GroupVolunteer groupVolunteer;
//        for (MemberOrg1 memberOrg1 : list) {
//            groupVolunteer = GroupVolunteer.build(memberOrg1);
//            if (memberOrg1.getOrgid() != null) {
//                Group group = groupDao.selectByOldId(Long.valueOf(memberOrg1.getOrgid()));
//                if (group != null) {
//                    groupVolunteer.setGroupId(group.getId());
//                }
//            }
//            if (memberOrg1.getMember() != null) {
//                Volunteer volunteer = volunteerDao.selectByOldId(memberOrg1.getMember());
//                if (volunteer != null) {
//                    groupVolunteer.setVolunteerId(volunteer.getId());
//                }
//            }
//            list1.add(groupVolunteer);
//        }
//        int a = groupVolunteerDao.insertList(list1);
//        return a;
    }
}
